package com.zdkj.subway.check.util;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.property.ExcelContentProperty;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

/**
 * LocalDate日期转换
 */
public class LocalDateConverter implements Converter<LocalDate>
{
    public Class<LocalDate> supportJavaTypeKey() {
        return LocalDate.class;
    }

    public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.STRING;
    }

    public LocalDate convertToJavaData(final CellData cellData, final ExcelContentProperty contentProperty, final GlobalConfiguration globalConfiguration) {
        return LocalDate.parse(cellData.getStringValue(), DateTimeFormatter.ofPattern("yyyy-MM-dd"));
    }

    public CellData<String> convertToExcelData(final LocalDate value, final ExcelContentProperty contentProperty, final GlobalConfiguration globalConfiguration) {
        return (CellData<String>)new CellData(value.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
    }
}
